/**
 * @file
 * @author dousha 
 * @date 2021/4/25.
 */

#ifndef PERISCOPE_DISPLAY_H
#define PERISCOPE_DISPLAY_H

#include <stdint.h>
#include "stm32f1xx_hal.h"

#define OLED_WRITE_ADDR 0x78u

#define OLED_SET_CONTRAST         0x81u
#define OLED_SET_ENTIRE_ON        0xa4u
#define OLED_SET_NORM_INV         0xa6u
#define OLED_SET_DISP             0xaeu
#define OLED_SET_MEM_ADDR         0x20u
#define OLED_SET_COL_ADDR         0x21u
#define OLED_SET_PAGE_ADDR        0x22u
#define OLED_SET_DISP_START_LINE  0x40u
#define OLED_SET_SEG_REMAP        0xa0u
#define OLED_SET_MUX_RATIO        0xa8u
#define OLED_SET_COM_OUT_DIR      0xc0u
#define OLED_SET_DISP_OFFSET      0xd3u
#define OLED_SET_COM_PIN_CFG      0xdau
#define OLED_SET_DISP_CLK_DIV     0xd5u
#define OLED_SET_PRECHARGE        0xd9u
#define OLED_SET_VCOM_DESEL       0xdbu
#define OLED_SET_CHARGE_PUMP      0x8du
#define OLED_SET_COM_SCAN_DEC     0xc8u
#define OLED_SET_NO_SCROLL        0x2eu
#define OLED_SET_DONE_AND_ENABLE  0xafu

#define OLED_USE_DMA_WRITE 0

typedef struct {
	uint8_t page;

	// page 0
	uint8_t mode;
	// all angles are in s3.1 format
	int16_t expectedYaw;
	int16_t expectedPitch;
	int16_t actualYaw;
	int16_t actualPitch;
	int16_t actualRoll;
	uint8_t joystickDirection;

	// page 1
	// all positions are in s5.4 format
	uint8_t gpsState;
	int32_t localLat;
	int32_t localLng;
	int32_t localAlt;
	int32_t remoteLat;
	int32_t remoteLng;
	int32_t remoteAlt;
	uint8_t satCount;

	// page 2
	int16_t magLow[3];
	int16_t magHigh[3];
	int16_t magRaw[3];
	int16_t magCorrected[3];
} DisplayInfo;

extern void display_init(I2C_HandleTypeDef *);

extern void display_set_cursor(uint8_t, uint8_t);

extern void display_printf(const char *, ...);

extern void display_clear();

extern void display_repaint();

extern DisplayInfo *display_get_info();

#endif //PERISCOPE_DISPLAY_H
